<?php
//蘑菇钉

if (!$_GET) {
   die();
} else {
   $usern=$_GET['username'];//账号
   $passw=$_GET['password'];//密码
   //签到位置信息
   $guojia=$_GET['country'];//国家
   $dizhi=$_GET['address'];//地址
   $sheng=$_GET['province'];//省
   $chengshi=$_GET['city'];//城市
   $neirong=$_GET['descr'];//发表的信息
   $type=$_GET['type'];//类型#START  上班  END 下班
   get_token($usern,$passw,$guojia,$dizhi,$sheng,$chengshi,$neirong,$type);
   //获取token
}
function get_token($zh,$mm,$gj,$dz,$she,$cs,$nr,$fs)
  {
    $template=array(
        "password"=>$mm,
        "phone"=>$zh,
        "loginType"=>"android",
        "uuid"=>""
    );
    $json_template=json_encode($template);
    $url12="https://api.moguding.net:9000/session/user/v1/login";//蘑菇钉请求地址
    $res=http_request($url12,urldecode($json_template),"");
    
    //res为返回的总数据包含token
    $new_token=token($res);
    // echo $new_token.'和';
    //数据返回在赋值变量中
    // echo "<br/><br/><br/><br/><br/>";
    $new_planid=planid($new_token);
    //将获取到的token代入换planid
    echo $new_planid;
    sign($new_planid,$new_token,$gj,$dz,$she,$cs,$nr,$fs);//执行签到
}
//http_request 利用curl请求 
function http_request($url,$data,$zdy){
    $ch = curl_init();//初始化
    curl_setopt($ch, CURLOPT_URL, $url);//设置
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: $zdy","Content-Type: application/json"));
    //设置Content-Type
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    // if ($zdy) {
    //   curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: $zdy","Content-Type: application/json"));
       
    // }
    // else if($zdy&&$kk){
    //     curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: $zdy","Content-Type: application/x-www-form-urlencoded"));
    // }
    // else{
    //     curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));
    // }
    $output = curl_exec($ch);//执行 
    curl_close($ch);//关闭
    return $output;//返回结果
}


  
function token($data1)
{
//返回码解析
$json_string = $data1;
$data = json_decode($json_string, true);
$token=$data['data']['token'];
return $token;

}
 
 function planid($new_toke)
{
    $templa=array(
        "state"=>""
    );
    $json_temp=json_encode($templa);
    $urll="https://api.moguding.net:9000/practice/plan/v1/getPlanByStu";//蘑菇钉请求地址
    $res=http_request($urll,urldecode($json_temp),$new_toke);
//解析
$json_string=$res;
$data = json_decode($json_string, true);
$new_planid=$data['data'][0]['planId'];
return $new_planid;

 }
 
 function sign($id,$tok,$guoj,$diz,$she,$chengs,$neiro,$ty)
{
    
$tem=array(
        "country"=>$guoj, //国家
        "address"=>$diz, //地址
        "province"=>$she, //省
        "city"=>$chengs, //城市
        "latitude"=>"", //纬度
        "description"=>$neiro, //发表的信息
        "planId"=>$id, //
        "type"=>$ty, //START上班 or END 下班
        "device"=>"android",
        "longitude"=>""//经度
    );
   
     $json_template=json_encode($tem);
    $url="https://api.moguding.net:9000/attendence/clock/v1/save";//蘑菇钉请求地址
    $re=http_request($url,urldecode($json_template),$tok);
    echo $re;
 }


//


?>